// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num1:0,
    num2:1,
    info:{},//商家信息
    count:0,
    carshow:false,//购物车显示
    show:false,
    sumcount:0,//购物车总数量   
    summon:0,//购物车总价格
    carmon:20,//配送费
    goods:[],
    carfoods:[],//购物车的商品
    carshow:false,//显示隐藏购物车
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      var id=options.id
      var id1=parseInt(id.substring(0,1))
      var id2=parseInt(id.substring(2))
      this.setData({num1:id1,num2:id2,})
    }
    var th=this
    wx.request({
      url: 'http://39.96.28.43:8000/data.json',
      method:"GET",
      header:"application/json",
      success:function(res){
        for(var i=0;i<res.data.goods.length;i++){
          for(var j=0;j<res.data.goods[i].foods.length;j++){
            for(var k=0;k<res.data.goods[i].foods[j].ratings.length;k++){
              var t=new Date(res.data.goods[i].foods[j].ratings[k].rateTime)
              var str=t.toLocaleString()
              res.data.goods[i].foods[j].ratings[k].rateTime=str
              res.data.goods[i].foods[j].count=0
            }
          }
        }
        var info=res.data.goods
        console.log(info)
        th.setData({
          info:info,
        })
      }
    })
  },
   //添加商品数量
   addcount(e){
    var i=parseInt(e.currentTarget.dataset.i);
    var j=parseInt(e.currentTarget.dataset.j); 
    var arr=this.data.info
    console.log(this.data)
    arr[i].foods[j].count+=1
    var num=++this.data.sumcount
    var mon=parseFloat(arr[i].foods[j].price)+this.data.summon
    this.setData({goods:arr,sumcount:num,summon:mon})
  },
  // 减去商品数量
  delcount:function(e){
    var i=parseInt(e.currentTarget.dataset.i);
    var j=parseInt(e.currentTarget.dataset.j);
    var arr=this.data.info
    arr[i].foods[j].count-=1
    var num=--this.data.sumcount
    var mon=this.data.summon-parseFloat(arr[i].foods[j].price)
    this.setData({goods:arr,sumcount:num,summon:mon})
  },
  // 打开购物车
  opencar(){
    this.setData({carshow:!this.data.carshow})
    var arr=[]
    for(var i=0;i<this.data.info.length;i++){
      for(var j=0;j<this.data.info[i].foods.length;j++){
        if(this.data.info[i].foods[j].count>0){
          arr.push(this.data.info[i].foods[j])
        }
      }
    }
    this.setData({carfoods:arr})
  },
  // 清空购物车
clearfood(){
  var th=this
  wx.showModal({
    title:'提示',
    content:'你确定要清空吗?',
    success(res){
      if(res.confirm){         
        var arr=th.data.info
        for(var i=0;i<arr.length;i++){
          for(var j=0;j<arr[i].foods.length;j++){
            arr[i].foods[j].count=0
          }
        }
          th.setData({
            carfoods:[],
            carshow:!th.data.carshow,
            sumcount:0,
            summon:0,
            goods:arr,
          })
      }
    }
  })
},
// 添加购物车里的商品
addcar(e){
  var num=e.currentTarget.dataset.num;
  var arr=this.data.carfoods;
  var name="";
  var count=0;
  arr[num].count+=1;
  name=arr[num].name; 
  count=arr[num].count;
  var good=this.data.info
  a: for(var i=0;i<good.length;i++){
    for(var j=0;j<good[i].foods.length;j++){
      if(name==good[i].foods[j].name){
        good[i].foods[j].count=count
        break a;
      }
    }
  }
  var sumcount=0
  var summon=0
  for(var k=0;k<arr.length;k++){
    summon+=parseFloat(arr[k].price)*parseFloat(arr[k].count)
    sumcount+=parseFloat(arr[k].count)
  }
  this.setData({goods:good,carfoods:arr})
},
//删除购物车里的商品
delcar(e){
  var num=e.currentTarget.dataset.num;
  var arr=this.data.carfoods;
  var name="";
  var count=0;
  arr[num].count-=1
  count=arr[num].count
  name=arr[num].name
  if(count<1){
    arr.splice(num,1)
  }
  if(arr.length<1){
    this.setData({carfoods:[],carshow:!th.data.carshow})
  }
  var good=this.data.info
  a: for(var i=0;i<good.length;i++){
    for(var j=0;j<good[i].foods.length;j++){
      if(name==good[i].foods[j].name){
        good[i].foods[j].count=count
        break a;
      }
    }
  }
  var sumcount=0
  var summon=0
  for(var k=0;k<arr.length;k++){
    summon+=parseFloat(arr[k].price)*parseFloat(arr[k].count)
    sumcount+=parseFloat(arr[k].count)
  }
  this.setData({goods:good,carfoods:arr,sumcount:sumcount,
    summon:summon,})
},
  // 显示
  xianshi(e){
    this.setData({show:!this.data.show})
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})